package ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.ck.hzq.R;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import base.BaseBiz;
import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import coom.MyType;
import event.CloseActEvent;
import event.HiddenOpenEvent;
import event.NetErrorEvent;
import event.SetUpdateEvent;
import ui.activity.profit.biz.ProfitBiz;
import ui.adapter.ProfitMoneyAdapter;
import ui.model.ProfitpromotionBean;

/**
 * Created by fangjunsong on 2019年3月19日17:03:30.
 * <p>
 * 推广奖励
 */

public class TodayMoneyPromotionFra extends LazyFragment{

    ProfitBiz biz;

    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;

    private int mpage = 1;
    private int msize = 100;
    ProfitMoneyAdapter adp;
    int year;
    int month;
    int day;
    List<ProfitpromotionBean.RecordsBean> dataInfos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        biz = new ProfitBiz();
        View view = inflater.inflate(R.layout.today_fraitem_layout, container, false);
        ButterKnife.bind(this, view);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(getActivity(),1));
        rv.setNoLoadMoreHideView(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                initData(year,month,day,mpage,msize);
            }
        });
        Calendar calendar = Calendar.getInstance();
//获取系统的日期
//年
         year = calendar.get(Calendar.YEAR);
//月
         month = calendar.get(Calendar.MONTH)+1;
//日
         day = calendar.get(Calendar.DAY_OF_MONTH);

        initData(year,month,day,mpage,msize);
        rv.addOnScrollListener(new MyRecyclerViewScrollListener());
        return view;
    }

    //滑动监听
    private class MyRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            EventBus.getDefault().post(new HiddenOpenEvent(0));
        }
    }
    //滑动监听
//    private class MyRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
//        @Override
//        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//            super.onScrollStateChanged(recyclerView, newState);
//            EventBus.getDefault().post(new HiddenOpenEvent(0));
////            LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
////            int lastVisibleItem = manager.findLastCompletelyVisibleItemPosition();//获取最后一个完全显示的ItemPosition
////            // 当不滚动时
////            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
////                // 判断是否滚动到顶部
////                if (lastVisibleItem == 0) {
////                    EventBus.getDefault().post(new HiddenOpenEvent(0));
////
////                }
////            } else if (newState == RecyclerView.SCROLL_STATE_DRAGGING ) {//拖动中
////                EventBus.getDefault().post(new HiddenOpenEvent(1));
////
////            }
//        }
//    }
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onSetUpdateEvent(SetUpdateEvent event) {
        mpage=1;
        year = event.getYear();
        month = event.getMonth();
        day = event.getDay();
        initData(year,month,day,mpage,msize);
    }
    private void initData(int year,int month,int day,int page,int size){
        biz.GetInComeDetailPromotion(year, month, day, page, size, new BaseBiz.Callback<ProfitpromotionBean>() {
            @Override
            public void onSuccess(ProfitpromotionBean profitCrahBean) {
                if(page == 1){
                    dataInfos.clear();
                }
                mpage+=1;
                if(profitCrahBean.getRecords().size()==msize){
                    rv.setHasLoadMore(true);
                }else{
                    rv.setHasLoadMore(false);
                }
                dataInfos.addAll(profitCrahBean.getRecords());

                if(null == adp) {
                    adp = new ProfitMoneyAdapter(getContext());
                    adp.setupData(dataInfos);
                    rv.setAdapter(adp);
                }else{
                    adp.setupData(dataInfos);
                }

            }

            @Override
            public void onFailure(ProfitpromotionBean profitCrahBean) {

            }
        });
    }
    @Override
    protected void lazyLoad() {
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.CompleteOrder) {
        }
    }




}
